import java.util.Random;
import java.util.Scanner;

public class GuessNumberNew {

    public static void main(String[] args) {
        System.out.println("请选择难度等级：\n1. 0~9\n2. 0~99\n3. 0~999\n0. 退出\n");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        if (a == 0)
            return;
        selectLevel(a);
    }

    public static void selectLevel(int n) {
        switch (n) {
            case 1:
                level(1);
                break;
            case 2:
                level(2);
                break;
            case 3:
                level(3);
                break;
        }
    }

    public static void level(int n) {
        int key = RandomInt.randomInt(0, (int) (Math.pow(10, n) - 1));
        Scanner sc = new Scanner(System.in);
        System.out.println("你当前选择的难度等级0~" + (int) (Math.pow(10, n) - 1)
                + "\n请输入想要猜的次数，0返回上一级\n你的输入：");


        int times = sc.nextInt();

        // 输入为0 就返回上一级。否则就作为次数进入循环

        if (times == 0) {
            System.out.println("请选择难度等级：\n1. 0~9\n2. 0~99\n3. 0~999\n0. 退出\n");
            int a = sc.nextInt();
            if (a == 0)
                return;
            selectLevel(a);
        }

        // 存放猜题记录
        int[] arr = new int[times];
        int i = 0;

        while (true) {
            System.out.println("剩余次数" + times + ",请输入数字(0~" + (int) (Math.pow(10, n) - 1)
                    + ") :");
            int number = sc.nextInt();
            arr[i++] = number;
            if (number < key)
                System.out.println("输入的是" + number + "，没有猜中，猜小了");
            else if (number > key)
                System.out.println("输入的是" + number + "，没有猜中，猜大了");
            else {
                System.out.println("输入的是" + number + "，恭喜你，猜中了");
                break;
            }

            times--;
            if (times == 0) {
                System.out.println("剩余次数为0，游戏结束！");
                break;
            }
        }

        System.out.println("猜题记录\n");
        for (int j = 0; j < i; j++) {
            if (arr[j] < key)
                System.out.println("第" + (j + 1) + "次 " + arr[j] + " => " + (arr[j] - key));
            else if (arr[j] > key)
                System.out.println("第" + (j + 1) + "次 " + arr[j] + " => +" + (arr[j] - key));
            else
                System.out.println("第" + (j + 1) + "次 " + arr[j] + " => " + "正确");
        }

        System.out.println("是否继续，1.继续, 0. 退出");
        int a;
        while (true) {
            a = sc.nextInt();
            if (a == 1) {
                System.out.println("请选择难度等级：\n1. 0~9\n2. 0~99\n3. 0~999\n0. 退出\n");
                int temp = sc.nextInt();
                if (temp != 0)
                    selectLevel(temp);
                break;
            } else if (a == 0) {
                break;
            } else {
                System.out.println("输入不合法！请重新输入：");
            }
        }


    }
}
